﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace BugSysSolution.AnalyzeXML
{
    public partial class xmlajaxrequest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String tag = Request["tag"] ?? String.Empty;
            if (tag == "update")
            {
                UpdateXml();
            }
            if (tag == "add")
            {
                AddXml();
            }
            if (tag == "del")
            {
                DelXml();
            }
        }

        public void UpdateXml()
        {
            String name = Request.Form["name"] ?? String.Empty;
            String age = Request.Form["age"] ?? String.Empty;
            String address = Request.Form["address"] ?? String.Empty;

            XmlDocument xmlDoc = new XmlDocument();
            String path = Server.MapPath(@"testxml.xml");
            xmlDoc.Load(path); 
            XmlNodeList nodelist = xmlDoc.SelectSingleNode("person").ChildNodes;
            foreach (XmlNode xn in nodelist) 
            {
                XmlElement xe = (XmlElement)xn;
                if (xe.GetElementsByTagName("name")[0].InnerText == name)
                {
                    xe.GetElementsByTagName("age")[0].InnerText = age;
                    xe.GetElementsByTagName("address")[0].InnerText = address;
                }
            }

            xmlDoc.Save(path);
            Response.Write("Success");
        }

        public void AddXml() 
        {
            String name = Request.Form["name"] ?? String.Empty;
            String age = Request.Form["age"] ?? String.Empty;
            String address = Request.Form["address"] ?? String.Empty;
            String job = Request.Form["job"] ?? String.Empty;
            String like = Request.Form["like"] ?? String.Empty;
            XmlDocument xmlDoc = new XmlDocument();
            string path = Server.MapPath(@"testxml.xml");
            xmlDoc.Load(path);
            XmlNode root = xmlDoc.SelectSingleNode("person");
            XmlElement xe1 = xmlDoc.CreateElement("student");

            XmlElement xesub1 = xmlDoc.CreateElement("name");
            xesub1.InnerText = name;
            xe1.AppendChild(xesub1);
            XmlElement xesub2 = xmlDoc.CreateElement("age");
            xesub2.InnerText = age;
            xe1.AppendChild(xesub2);
            XmlElement xesub3 = xmlDoc.CreateElement("address");
            xesub3.InnerText = address;
            xe1.AppendChild(xesub3);
            XmlElement xesub4 = xmlDoc.CreateElement("job");
            xesub4.InnerText = job;
            xe1.AppendChild(xesub4);
            XmlElement xesub5 = xmlDoc.CreateElement("like");
            xesub5.InnerText = like;
            xe1.AppendChild(xesub5);

            root.AppendChild(xe1);
            xmlDoc.Save(path);
            Response.Write("Success");
        }

        public void DelXml()
        {
            String name = Request.Form["name"] ?? String.Empty;
            XmlDocument xmlDoc = new XmlDocument();
            String path = Server.MapPath(@"testxml.xml");
            xmlDoc.Load(path);
            XmlNode root = xmlDoc.SelectSingleNode("person");
            XmlNodeList nodelist = xmlDoc.SelectSingleNode("person").ChildNodes;
            for (int i = 0; i < nodelist.Count; i++)
            {
                XmlElement xe = (XmlElement)nodelist.Item(i);
                if (xe.GetElementsByTagName("name")[0].InnerText == name)
                {
                    root.RemoveChild(xe);
                }
            }
            xmlDoc.Save(path);
            Response.Write("Success");
        }
    }
}